﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.DAL
{
    public class DepartmentDAL:IDepartmentDAL
    {
        #region IDepartmentDAL 成员

        public IDataReader Select(int DepartmentNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Reader(ConnectionString.ConnString, "Proc_Department_Select", new SqlParameter[] { new SqlParameter("@DepartmentNum", DepartmentNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }         
        }

        public DataTable Select()
        {
            try
            {
                return SqlDbHelper.RunProcedure_DataTable(ConnectionString.ConnString, "Proc_Department_Select_All");
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Insert(string DepartmentName, string DepartmentCode)
        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@DepartmentName",DepartmentName),
                    new SqlParameter("@DepartmentCode",DepartmentCode)
                };
                 return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Department_Insert", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Update(int DepartmentNum, string DepartmentName, string DepartmentCode)
        {
            try
            {
                SqlParameter[] all = new SqlParameter[]
                {
                    new SqlParameter("@DepartmentNum",DepartmentNum),
                    new SqlParameter("@DepartmentName",DepartmentName),
                    new SqlParameter("@DepartmentCode",DepartmentCode)
                };
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Department_Update", all);
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        public int Delete(int DepartmentNum)
        {
            try
            {
                return SqlDbHelper.RunProcedure_Query(ConnectionString.ConnString, "Proc_Department_Delete", new SqlParameter[] { new SqlParameter("@DepartmentNum", DepartmentNum) });
            }
            catch (Exception ex)
            {
                EventLog.WriteErrorLog(ex.Message);
                throw;
            }
        }

        #endregion
    }
}
